package com.zfei.gfb.request;

import cn.hutool.core.lang.Validator;
import com.zfei.gfb.bean.Const;
import com.zfei.gfb.util.ObjUtil;
import com.zfei.gfb.bean.Status;
import com.zfei.gfb.bean.StatusException;
import io.swagger.annotations.ApiModelProperty;
import org.apache.commons.lang3.StringUtils;

import java.util.List;


/**
 * @author fgh
 */
public class GfbHouseEditRequest {

    // id
    @ApiModelProperty(value = "房子id", required = true)
    private Integer id;
    // 房源名称
    @ApiModelProperty(value = "房源名称", required = true)
    private String name;
    // 户型
    @ApiModelProperty(value = "户型， 例如2室1厅1卫，传“2-1-1”", required = true)
    private String unitType;

    @ApiModelProperty(hidden = true)
    private Integer unit;
    // 图片url集合,json数组形式
    @ApiModelProperty(value = "图片数组", required = true)
    private List<String> images;
    // 建筑面积
    @ApiModelProperty(value = "建筑面积", required = true)
    private Float area;
    // 朝向
    @ApiModelProperty(value = "朝向， 北0南1西2东3", required = true)
    private Integer face;
    // 联系人电话
    @ApiModelProperty(value = "联系人电话", required = true)
    private String phone;
    // 月租金
    @ApiModelProperty(value = "月租金", required = true)
    private Integer price;
    // 出租方式
    @ApiModelProperty(value = "出租方式：2:长租 1:短租", required = false)
    private Integer type;
    // 房源地图坐标
    @ApiModelProperty(value = "坐标", required = true)
    private String point;
    // 房源地址
    @ApiModelProperty(value = "详细地址", required = true)
    private String address;
    // 房源描述
    @ApiModelProperty(value = "房源描述", required = true)
    private String content;

    public Integer getUnit() {
        return unit;
    }

    public void setUnit(Integer unit) {
        this.unit = unit;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }
    public void setName(String value) {
        this.name = value;
    }

    public String getName() {
        return this.name;
    }

    public void setUnitType(String value) {
        this.unitType = value;
    }

    public String getUnitType() {
        return this.unitType;
    }

    public List<String> getImages() {
        return images;
    }

    public void setImages(List<String> images) {
        this.images = images;
    }

    public void setArea(Float value) {
        this.area = value;
    }

    public Float getArea() {
        return this.area;
    }

    public Integer getFace() {
        return face;
    }

    public void setFace(Integer face) {
        this.face = face;
    }

    public void setPhone(String value) {
        this.phone = value;
    }

    public String getPhone() {
        return this.phone;
    }

    public void setPrice(Integer value) {
        this.price = value;
    }

    public Integer getPrice() {
        return this.price;
    }

    public void setType(Integer value) {
        this.type = value;
    }

    public Integer getType() {
        return this.type;
    }

    public void setPoint(String value) {
        this.point = value;
    }

    public String getPoint() {
        return this.point;
    }

    public void setAddress(String value) {
        this.address = value;
    }

    public String getAddress() {
        return this.address;
    }

    public void setContent(String value) {
        this.content = value;
    }

    public String getContent() {
        return this.content;
    }


    public void checkInsertNull() {
        if (ObjUtil.anyNull(id, name, unitType, images, area, face, phone, price, point, address, content, type)) {
            throw new StatusException(Status.INVALID_ARGUMENT);
        }
        if(StringUtils.isAnyBlank(name, phone, address, content)){
            throw new StatusException(Status.INVALID_ARGUMENT);
        }
        if(name.length() > Const.NAME_LENGTH){
            throw new StatusException(Status.INVALID_ARGUMENT);
        }
        if(unitType.split("-").length != 3){
            throw new StatusException(Status.INVALID_ARGUMENT);
        }
        if(area <= 0){
            throw new StatusException(Status.INVALID_ARGUMENT);
        }
        if(area < Const.HOUSE_AREA && type != Const.SHORT_RENTALS){
            throw new StatusException(Status.INVALID_ARGUMENT);
        }
        if(area >= Const.HOUSE_AREA && type != Const.LONG_RENTALS){
            throw new StatusException(Status.INVALID_ARGUMENT);
        }
        if(!Const.legalFace(face)){
            throw new StatusException(Status.INVALID_ARGUMENT);
        }
//        if(!Validator.isMobile(phone)){
//            throw new StatusException(Status.INVALID_ARGUMENT);
//        }
        if(price <= 0){
            throw new StatusException(Status.INVALID_ARGUMENT);
        }
        if(content.length() > Const.CONTENT_LENGTH){
            throw new StatusException(Status.INVALID_ARGUMENT);
        }
        if(images.size() > Const.IMAGE_SIZE){
            throw new StatusException(Status.INVALID_ARGUMENT);
        }
        for (String image : images) {
            if (!Validator.isUrl(image)) {
                throw new StatusException(Status.INVALID_ARGUMENT);
            }
        }
        try {
            unit = Integer.valueOf(unitType.split("-")[0]);
        }catch (Exception e){
            throw new StatusException(Status.INVALID_ARGUMENT);
        }
    }

}

